package C;

import java.util.Arrays;
import java.util.Scanner;

public class C26 {
    static class Country {
        String name;
        int gold;
        int silver;
        int bronze;

        public Country(String name, int gold, int silver, int bronze) {
            this.name = name;
            this.gold = gold;
            this.silver = silver;
            this.bronze = bronze;
        }
    }


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Country[] countries = new Country[n];
        for (int i=0;i<n;i++) {
            countries[i] = new Country(sc.next(),sc.nextInt(),sc.nextInt(),sc.nextInt());
        }
        getResult(countries);

    }

    public static void getResult(Country[] countries) {
        Arrays.sort(countries,
                (a,b)-> a.gold!=b.gold ? b.gold-a.gold : a.silver!=b.silver ? b.silver-a.silver :
                        a.bronze!=b.bronze ? b.bronze-a.bronze : a.name.compareTo(b.name));

        for (int i=0;i<countries.length;i++) {
            System.out.println(countries[i].name);
        }




    }
}
